<?php

namespace app\index\service;

use think\Db;

/**
 * 报表管理----销售报表
 * @author xjy
 */
class SaleoreportService
{
        private static $salesState=["1"=>"新产品","2"=>"清仓品","3"=>"在售品","4"=>"下架品"];  //销售状态
        private static $cycleStage=["1"=>"补货期","2"=>"新品期","3"=>"计划淘汰期","4"=>"淘汰期"];  //生命周期
    
    //SKU生命周期列表
    static public function getStoreSkuList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
            $titles=[
                 'shop_name'=>'店铺','sku'=>'SKU','cname'=>'中文名称','sku_state'=>'SKU状态','cycle_stage'=>'备货状态','daily_average'=>'日均销量',
                 '7day_average'=>'7天销量','14day_average'=>'14天销量','21day_average'=>'21天销量','28day_average'=>'28天销量',
                 'year_saleamount'=>'年总销量','total_money'=>'总销售额（¥）','saleamount'=>'累计销量'];
        }
        $groupby='id';
        return \app\index\model\SaleoreportModel::getList($param,$titles,"app\index\service\SaleoreportService::convertData",$groupby);  
    }
     static public function getCountrySkuList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
            $titles=[
                 'country_name'=>'国家','sku'=>'SKU','cname'=>'中文名称','sku_state'=>'SKU状态','cycle_stage'=>'备货状态','daily_average'=>'日均销量',
                 '7day_average'=>'7天销量','14day_average'=>'14天销量','21day_average'=>'21天销量','28day_average'=>'28天销量',
                 'year_saleamount'=>'年总销量','total_money'=>'总销售额（¥）','saleamount'=>'累计销量'];
        }
        $groupby="concat(country,'_',sku)";
        return \app\index\model\SaleoreportModel::getList($param,$titles,"app\index\service\SaleoreportService::convertData",$groupby);  
    }
     static public function getSkuSaleList($param)
    {
        if(empty($param['down'])){
             $titles=[];
        }else{
             $titles=[
                 'sku'=>'SKU','cname'=>'中文名称','sku_state'=>'SKU状态','cycle_stage'=>'备货状态','daily_average'=>'日均销量',
                 '7day_average'=>'7天销量','14day_average'=>'14天销量','21day_average'=>'21天销量','28day_average'=>'28天销量',
                 'year_saleamount'=>'年总销量','total_money'=>'总销售额（¥）','saleamount'=>'累计销量'];
        }
        $groupby="sku";
        return \app\index\model\SaleoreportModel::getList($param,$titles,"app\index\service\SaleoreportService::convertData",$groupby);  
    }
    public static function convertData($data,$param){
        $country=\app\common\ConfigBase::getCountry();
        $shop=\app\common\ConfigBase::getShop();
        $totalCost= \app\common\ConfigBase::getDeliveryTotalCost();
        if(!empty($data)){
            foreach($data as $key=>&$value){
                $value['country_name']=isset($country[$value['country']])? $country[$value['country']]:'';   
                $value['shop_name']=isset($shop[$value['shop']])? $shop[$value['shop']]:'';  
                $value['total_cost_money']=isset($totalCost[$value['shop']."_".$value['sku']])? $totalCost[$value['shop']."_".$value['sku']]:0;   
            }
        }
        return $data;    
    }
}
